{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Length of Last Word"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: lengthOfLastWord"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最后一个单词的长度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串 <code>s</code>，由若干单词组成，单词前后用一些空格字符隔开。返回字符串中 <strong>最后一个</strong> 单词的长度。</p>\n",
    "\n",
    "<p><strong>单词</strong> 是指仅由字母组成、不包含任何空格字符的最大子字符串。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"Hello World\"\n",
    "<strong>输出：</strong>5\n",
    "<strong>解释：</strong>最后一个单词是“World”，长度为5。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"   fly me   to   the moon  \"\n",
    "<strong>输出：</strong>4<strong>\n",
    "解释：</strong>最后一个单词是“moon”，长度为4。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"luffy is still joyboy\"\n",
    "<strong>输出：</strong>6\n",
    "<strong>解释：</strong>最后一个单词是长度为6的“joyboy”。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>s</code> 仅有英文字母和空格 <code>' '</code> 组成</li>\n",
    "\t<li><code>s</code> 中至少存在一个单词</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [length-of-last-word](https://leetcode.cn/problems/length-of-last-word/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [length-of-last-word](https://leetcode.cn/problems/length-of-last-word/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"Hello World\"', '\"   fly me   to   the moon  \"', '\"luffy is still joyboy\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:     \n",
    "        return len(s.strip().split(\" \")[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        list1 = s.split() \n",
    "        if list1 != []: return len(list1[-1]) \n",
    "        else: return 0\n",
    "        \n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: 'str') -> 'int':\n",
    "        n = len(s)\n",
    "        if n == 0:\n",
    "            return 0\n",
    "        count = 0\n",
    "        k = n - 1\n",
    "        while s[k] == ' ' and k:\n",
    "            k -= 1\n",
    "        for i in range(k, -1, -1):\n",
    "            if s[i] != ' ':\n",
    "                count += 1\n",
    "            else:\n",
    "                break\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        leng = 0\n",
    "        for i in s[::-1]:\n",
    "            if i!=' ':\n",
    "                leng += 1\n",
    "            elif leng != 0:\n",
    "                return leng\n",
    "            else:\n",
    "                continue\n",
    "        return leng\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        save=[]\n",
    "        save=list(filter(None,s.split(' ')))\n",
    "        if len(save)==0:\n",
    "            return 0\n",
    "        else:\n",
    "            num=len(save)\n",
    "            strlen=len(save[num-1])\n",
    "            return strlen\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        s = s.strip()\n",
    "        if s == \"\":\n",
    "            return 0\n",
    "        elif \" \" not in s:\n",
    "            return len(s)\n",
    "        else:\n",
    "            return len(s.split()[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        s=s.strip()\n",
    "        lst=s.split(' ')\n",
    "        length=len(lst)\n",
    "        return len(lst[length-1])\n",
    "        #lst=s.split(' ')\n",
    "        #length=len(lst)\n",
    "        #\n",
    "        #if '' in lst:        \n",
    "        #    for i,j in enumerate(lst):\n",
    "        #        if j=='':\n",
    "        #            lst.pop(i)\n",
    "        #            length-=1\n",
    "        #return len(lst[length-1])\n",
    "        #if '' not in lst:\n",
    "        #    \n",
    "        #    return len(lst(length-1))\n",
    "        #return len(lst[length-1])\n",
    "                \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        result = 0\n",
    "        for char in reversed(s.strip()):\n",
    "            if char != ' ': result += 1\n",
    "            else: break\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        try:\n",
    "            split_words = s.split()\n",
    "            return len(split_words[-1])\n",
    "        except:\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if len(s) == 0:\n",
    "            return 0\n",
    "        word = ''\n",
    "        wordSet = []\n",
    "        for ele in s:\n",
    "            if ele != ' ':\n",
    "                word += ele\n",
    "            else:\n",
    "                if len(word.strip()) > 0:\n",
    "                    wordSet.append(word)\n",
    "                    word = ''\n",
    "        if len(word.strip()) > 0:\n",
    "            wordSet.append(word)\n",
    "        \n",
    "        return len(wordSet[-1]) if len(wordSet) > 0 else 0\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        word = []\n",
    "        #pre = []\n",
    "        if(s == ' '): return 0\n",
    "        length = 0\n",
    "        for char in s:\n",
    "            print(\"_\" + char)\n",
    "            if(char == ' '): \n",
    "                word.clear()\n",
    "            else: \n",
    "                word.append(char)\n",
    "                pre = word\n",
    "                length = len(pre)\n",
    "        return length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        q=''\n",
    "        cur=''\n",
    "        for x in s:\n",
    "            if x!=' ':\n",
    "                q=q+x\n",
    "            else:\n",
    "                if q!='':\n",
    "                    cur=q\n",
    "                q=''\n",
    "        if q=='':\n",
    "            q=cur\n",
    "        return len(q)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "#length-of-last-word\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        s = s.strip()\n",
    "        sum = ''\n",
    "        for i in s:\n",
    "            if i == ' ':\n",
    "                sum = ''\n",
    "            else:\n",
    "                sum += i\n",
    "        return len(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: 'str') -> 'int':\n",
    "        if s=='':\n",
    "            return 0\n",
    "        word=''\n",
    "        i = len(s)-1\n",
    "        flag=0\n",
    "        while(i>=0):\n",
    "            # print(s[i],word)\n",
    "            if s[i] != ' ':\n",
    "                flag=1\n",
    "                word = s[i]+word\n",
    "                i-=1\n",
    "            else:\n",
    "                if flag == 1:\n",
    "                    break\n",
    "                else:\n",
    "                    i-=1\n",
    "        return len(word)\n",
    "                \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        return len(s.strip(' ').split(' ')[-1])\n",
    "        #res = [i for i in s.rstrip(' ').split(' ')]\n",
    "        #return len(res[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        s = s.strip()\n",
    "        return len(s.split(' ')[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int: #最后一个单词之后有空格\n",
    "        if not s:\n",
    "            return 0\n",
    "        cnt, tail = 0, len(s) - 1\n",
    "        while tail >= 0 and s[tail] == ' ':\n",
    "            tail -= 1 # 去除末尾空格\n",
    "        while tail>= 0 and s[tail] != ' ': #从后往前遍历\n",
    "            tail -= 1\n",
    "            cnt += 1\n",
    "        return cnt\n",
    "        # s = s.rstrip().split(' ')\n",
    "        # return len(s[-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        words = s.split(' ')\n",
    "        for word in words[::-1]:\n",
    "            if word != '':\n",
    "                return len(word)\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        str_list = s.strip().split()\n",
    "        if not str_list:\n",
    "            return 0\n",
    "        else:\n",
    "            return len(str_list[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def lengthOfLastWord(self, s: str) -> int:\n",
    "    len_ = 0\n",
    "\n",
    "    for idx in range(len(s) - 1, -1, -1):\n",
    "      if s[idx] != ' ':\n",
    "        len_ += 1\n",
    "      elif s[idx] == ' ' and len_ > 0:\n",
    "        break\n",
    "\n",
    "    return len_\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def lengthOfLastWord(self, s):\n",
    "        s = s.strip()\n",
    "        if len(s) != 0:\n",
    "            return len(s.split(' ')[-1])\n",
    "        else:\n",
    "            return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        if not s : return 0\n",
    "        a=len(s)-1\n",
    "        b=0\n",
    "        for i in range(a,-1,-1):\n",
    "            if s[i]!=' ':\n",
    "                b+=1\n",
    "            elif b!=0:\n",
    "                return b\n",
    "        return b\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:      \n",
    "        index_1 = []\n",
    "        index_2 = []\n",
    "        for i,n in enumerate(s):\n",
    "            if s[i].isspace() == True:\n",
    "                index_1 = index_1 + [i]\n",
    "            else:\n",
    "                index_2 = index_2 + [i]\n",
    "        print(index_1)\n",
    "        print(index_2)\n",
    "        if len(index_1) == 0:\n",
    "            return len(s)\n",
    "        elif len(index_2) == 0:\n",
    "            return 0\n",
    "        else:\n",
    "            t = []\n",
    "            for x in index_1:\n",
    "                if x < index_2[-1]:\n",
    "                    t = t + [x]\n",
    "            print(t)\n",
    "            if len(t) == 0:\n",
    "                return len(index_2)\n",
    "            else:\n",
    "                return index_2[-1] - max(t)\n",
    "\n",
    "            \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "                \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        if not s:\n",
    "            return 0\n",
    "        s = \" \"+s\n",
    "        while s[-1] == \" \":\n",
    "            s = s[:-1]\n",
    "            if not s:\n",
    "                return 0\n",
    "        for inx, i in list(enumerate(s))[::-1]:\n",
    "            if i == \" \":\n",
    "                return len(s[inx+1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        return len(s.split()[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        ls = len(s)\n",
    "        ans = 0\n",
    "        for i in range(ls-1,-1,-1):\n",
    "            if s[i]==\" \":\n",
    "                if ans >0:\n",
    "                    return ans\n",
    "            else:\n",
    "                ans+=1\n",
    "        if i == 0:\n",
    "            return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        ans = 0\n",
    "        for ch in range(n-1,-1,-1):\n",
    "            if s[ch] != ' ':\n",
    "                ans += 1\n",
    "            else:\n",
    "                if ans:\n",
    "                    return ans\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n",
    "        '''\n",
    "        res = s.split()\n",
    "        return len(res[-1])\n",
    "        '''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        last_work = s.strip().split(\" \")[-1]\n",
    "        return len(last_work)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def lengthOfLastWord(self, s):\r\n",
    "\r\n",
    "        # s = s.strip()\r\n",
    "        # return len(s.split(' ')[-1])\r\n",
    "\r\n",
    "        length = 0\r\n",
    "        for i in s[: :-1]:\r\n",
    "            print(i, length)\r\n",
    "            if i != ' ' :\r\n",
    "                length += 1\r\n",
    "            else:\r\n",
    "                if length:\r\n",
    "                    break\r\n",
    "                else:\r\n",
    "                    continue\r\n",
    "        return length\r\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        # # list1 = list(s,)\n",
    "        # length = len(s.split()[-1])\n",
    "        # print(s.split())\n",
    "        # # llist = split(list1,' ')\n",
    "        # return length\n",
    "        l = len(s)\n",
    "        flag = 0 \n",
    "        print(list(range(l-1, 1, -1)))\n",
    "        for i in range(l-1, -1, -1):\n",
    "            if s[i] != ' ':\n",
    "                flag += 1\n",
    "            else:\n",
    "                if flag:\n",
    "                    return flag\n",
    "\n",
    "        return flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        return len(s.split()[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        s= s.strip()\n",
    "        s = s[::-1]\n",
    "        temp = 0\n",
    "        for item in s:\n",
    "            if item ==\" \":\n",
    "                break\n",
    "            else:\n",
    "                temp = temp + 1\n",
    "        return temp\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def lengthOfLastWord(self, s: str) -> int:\n",
    "        words = s.strip().split(\" \")\n",
    "        return len(words[-1])\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
